﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.Classes;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using System.IO;
using System.Text;

namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class NullableCharacteristic : UserControl
    {
        private IndexMarkServiceReference.IndexMarkServiceClient client = new IndexMarkServiceClient();

        private int IDTerm;

        private ObservableCollection<NullChar> _chars;

        public PagedCollectionView Chars
        {
            get
            {
                PagedCollectionView ind = new PagedCollectionView(this._chars);
                ind.GroupDescriptions.Clear();
                return ind;
            }
        }

        public NullableCharacteristic()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(IndexListLoaded);
        }

        void IndexListLoaded(object sender, RoutedEventArgs e)
        {
            var vm = MainPage.ViewModel;
            vm.SelectTerm -= ViewModel_SelectTerm;
            vm.SelectTerm += new MainViewModel.SelectTermEventHandler(ViewModel_SelectTerm);
            vm.SelectTermInit();
            client.GetNullableCharsCompleted += new EventHandler<GetNullableCharsCompletedEventArgs>(getNullableComlited);
            client.GetNullableCharsAsync(IDTerm);
        }

        void getNullableComlited(object sender, GetNullableCharsCompletedEventArgs e)
        {
            if (e.Result != null) _chars = e.Result;
            DataContext = Chars;
        }


        void ViewModel_SelectTerm(object sender, SelectTermEventsArgs e)
        {
            IDTerm = e.SelectTerm.IDTerm;
        }

        private void txtBoxFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            (CharacteristicGrid.ItemsSource as PagedCollectionView).Filter = (obj) =>
            {
                return string.IsNullOrEmpty(
                    (sender as TextBox).Text) || (obj as NullChar).Name.
                    ToLower().Contains((sender as TextBox).Text.ToLower());
            };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ExportToExel(_chars);
        }

        private void ExportToExel(ObservableCollection<NullChar> _chars)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Простой файл электронных таблиц | *.csv";
            if (sfd.ShowDialog().Value)
                using (StreamWriter SW = new StreamWriter(sfd.OpenFile(), CConst.Encoding))
                {
                    SW.Write("№;");
                    SW.Write("Показатель;");
                    SW.Write("Ответственные;");
                    SW.WriteLine();

                    foreach (var row in _chars)
                    {
                        SW.Write(row.Order + ";");
                        SW.Write(row.Name + ";");
                        SW.Write(row.Responsibles + ";");
                        SW.WriteLine();
                    }

                    SW.Close();
                }
        }
    }
}